package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	import com.UI.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.table.*;
	import org.aswing.tree.*;
	public class FrameFriendChat extends JFrame
	{
		//members define
		private var txtFriend:JTextArea;
		private var imgFriend:JPanel;
		private var txtSelf:JTextArea;
		private var imgSelf:JPanel;
		private var panel236:JPanel;
		private var labelPrompt:JLabel;
		private var btn_friend_chat_motion:JButton;
		private var btn_friend_chat_close:JButton;
		private var btn_friend_chat_send:JButton;
		private var labelMessage:JLabel;
		private var labelFriendName:JLabelButton;
		private var labelFriendRace:JLabel;
		private var labelFriendGender:JLabel;
		//--------------------------
		private var btn_close:JButton;
		public static var _frame:FrameFriendChat = null;
		public var _item:Object = null;
		public static var _messageArray:Array  = new Array();
		private var _loader:LoaderData;
		public function FrameFriendChat(item:Object)
		{
			super();
			//setClosable(false);
			setResizable(false);  
			setBorder(new EmptyBorder());
			
			this.setClipMasked(false);
			getContentPane().setClipMasked(false);
			getTitleBar().getFrame().setClipMasked(false);
			
			_item = item;
			
			//component creation
			setSize(new IntDimension(367, 275));
			setPreferredSize(new IntDimension(367, 275));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			txtFriend = new JTextArea();
			txtFriend.setLocation(new IntPoint(7, 2));
			txtFriend.setSize(new IntDimension(242, 144));
			txtFriend.setPreferredSize(new IntDimension(242, 144));
			
			imgFriend = new JPanel();
			imgFriend.setLocation(new IntPoint(270, 7));
			imgFriend.setSize(new IntDimension(69, 69));
			imgFriend.setPreferredSize(new IntDimension(69, 69));
			
			txtSelf = new JTextArea();
			txtSelf.setLocation(new IntPoint(7, 147));
			txtSelf.setSize(new IntDimension(242, 94));
			txtSelf.setPreferredSize(new IntDimension(242, 94));
			
			imgSelf = new JPanel();
			imgSelf.setLocation(new IntPoint(270, 170));
			imgSelf.setSize(new IntDimension(69, 69));
			imgSelf.setPreferredSize(new IntDimension(69, 69));
			
			panel236 = new JPanel();
			panel236.setLocation(new IntPoint(7, 243));
			panel236.setSize(new IntDimension(242, 25));
			panel236.setPreferredSize(new IntDimension(242, 25));
			var layout1:FlowLayout = new FlowLayout();
			layout1.setAlignment(AsWingConstants.RIGHT);
			layout1.setHgap(5);
			layout1.setVgap(0);
			layout1.setMargin(true);
			panel236.setLayout(layout1);
			
			labelPrompt = new JLabel();
			labelPrompt.setLocation(new IntPoint(5, 0));
			labelPrompt.setSize(new IntDimension(94, 25));
			labelPrompt.setPreferredSize(new IntDimension(94, 25));
			labelPrompt.setText("Ctrl+Enter发送");
			
			btn_friend_chat_motion = new JButton();
			btn_friend_chat_motion.setLocation(new IntPoint(66, 0));
			btn_friend_chat_motion.setSize(new IntDimension(25, 25));
			btn_friend_chat_motion.setPreferredSize(new IntDimension(25, 25));
			btn_friend_chat_motion.setText("label");
			
			btn_friend_chat_close = new JButton();
			btn_friend_chat_close.setLocation(new IntPoint(104, 1));
			btn_friend_chat_close.setSize(new IntDimension(49, 25));
			btn_friend_chat_close.setPreferredSize(new IntDimension(49, 25));
			btn_friend_chat_close.setText("label");
			
			btn_friend_chat_send = new JButton();
			btn_friend_chat_send.setLocation(new IntPoint(160, 1));
			btn_friend_chat_send.setSize(new IntDimension(49, 25));
			btn_friend_chat_send.setPreferredSize(new IntDimension(49, 25));
			btn_friend_chat_send.setText("label");
			
			labelMessage = new JLabel();
			labelMessage.setLocation(new IntPoint(7, -20));
			labelMessage.setSize(new IntDimension(26, 17));
			labelMessage.setText("label");
			
			labelFriendName = new JLabelButton();
			labelFriendName.setLocation(new IntPoint(254, 86));
			labelFriendName.setSize(new IntDimension(102, 16));
			labelFriendName.setPreferredSize(new IntDimension(102, 16));
			labelFriendName.setText("陶陶陶 Lv.58");
			
			labelFriendRace = new JLabel();
			labelFriendRace.setLocation(new IntPoint(254, 103));
			labelFriendRace.setSize(new IntDimension(102, 16));
			labelFriendRace.setPreferredSize(new IntDimension(102, 16));
			labelFriendRace.setText("种族:人");
			
			labelFriendGender = new JLabel();
			labelFriendGender.setLocation(new IntPoint(254, 120));
			labelFriendGender.setSize(new IntDimension(102, 16));
			labelFriendGender.setPreferredSize(new IntDimension(102, 16));
			labelFriendGender.setText("性别:男");
			
			//component layoution
			getContentPane().append(txtFriend);
			getContentPane().append(imgFriend);
			getContentPane().append(txtSelf);
			getContentPane().append(imgSelf);
			getContentPane().append(panel236);
			getContentPane().append(labelMessage);
			getContentPane().append(labelFriendName);
			getContentPane().append(labelFriendRace);
			getContentPane().append(labelFriendGender);
			
			panel236.append(labelPrompt);
			panel236.append(btn_friend_chat_motion);
			panel236.append(btn_friend_chat_close);
			panel236.append(btn_friend_chat_send);

			//------------------------------
			
			btn_close = new JButton();
			btn_close.setLocation(new IntPoint(82, 0));
			btn_close.setSize(new IntDimension(27, 27));
			btn_close.setPreferredSize(new IntDimension(27, 27));
			this.getTitleBar().setCloseButton(btn_close);
			labelMessage.setText("与 "+_item.m_szCharName+" 聊天中");
			labelMessage.pack();
			//setTitle("与 "+_item.m_szCharName+" 聊天中");
			this.setClipMasked(false);
			this.getContentPane().setClipMasked(false);
			setImages();
			pack();

			//update();
		}
		
		/*private function onLoaded(e:Event):void
		{
			var l:LoaderInfo=e.target as LoaderInfo;
			l.removeEventListener(Event.COMPLETE,onLoaded);
			l.removeEventListener(IOErrorEvent.IO_ERROR,onLoadedError);
			var loader:LoaderData = l.loader as LoaderData;
			
			if(_mcCharHeadFriend != null && imgSelf.contains(_mcCharHeadFriend))
			{
				imgSelf.removeChild(_mcCharHeadFriend)
				_mcCharHeadFriend = null;
			}
			var _appDomain:ApplicationDomain = _loader.contentLoaderInfo.applicationDomain;
		
			_mcCharHeadFriend = Engine.getElement(_appDomain,"mc_object_stand") as MovieClip;//e.target.content as MovieClip;//
			if(_mcCharHeadFriend == null)
				return;
			_mcCharHeadFriend.gotoAndStop(0);
			_mcCharHeadFriend.scaleX = 2;
			_mcCharHeadFriend.scaleY = 2;
			_mcCharHeadFriend.x = (imgFriend.width - _mcCharHeadFriend.width)/2;//+mcTmp.width/2;

			if(_item.m_nGender == 0)
				_mcCharHeadFriend.y = Client._headOffsetYBoy*_mcCharHeadFriend.scaleY;//mc.height;
			else
				_mcCharHeadFriend.y = Client._headOffsetYGirl*_mcCharHeadFriend.scaleY;
			imgFriend.addChild(_mcCharHeadFriend);

			if(_loader != null)
			{
				_loader.unloadAndStop();
				_loader = null;
			}
			loader.unloadAndStop();
			loader = null;
		}
		
		private function onLoadedError(e:IOErrorEvent):void
		{
			try
			{
				var l:LoaderInfo=e.target as LoaderInfo;
				l.removeEventListener(Event.COMPLETE,onLoaded);
				l.removeEventListener(IOErrorEvent.IO_ERROR,onLoadedError);
				var loader:LoaderData = l.loader as LoaderData;
				if(_loader != null)
				{
					_loader.unloadAndStop();
					_loader = null;
				}
				loader.unloadAndStop();
				loader = null;
			}
			catch(e:Error)
			{
				trace("onLoadedError")
			}
		}*/
		public function update():void
		{
			_frame.txtFriend.setHtmlText(_messageArray[_item.m_nCharId]);
			_frame.txtFriend.scrollToBottomLeft();
			_frame.labelMessage.setText("与 "+_item.m_szCharName+" 聊天中");
			labelFriendName.setText(_item.m_szCharName+' Lv.'+_item.m_nLevel);
			//_frame.setTitle("与 "+_item.m_szCharName+" 聊天中");
			
			labelFriendRace.setText('种族:'+(int(_item.m_nRace) == 0 ? '人':((_item.m_nRace) == 1 ?'魔':'仙')));
			labelFriendGender.setText('性别:'+(int(_item.m_nGender) == 0 ? '男':'女'));
			labelFriendName.setForeground(new ASColor(int(_item.m_nGender) == 0 ? Engine.colorBoy : Engine.colorGirl));
			_frame.labelMessage.pack();
			_frame.pack();
			
			/*if(_loader != null)
			{
				_loader.unloadAndStop();
				_loader = null;
			}
			_loader = new LoaderData();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadedError);
			_loader.load(new URLRequest(Engine.assetCharPath + "char_"+ _item.m_nGender + '0.swf'));*/
		}
		
		public function setImages():void
		{
			imgFriend.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_head") as DisplayObject));
			imgSelf.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_head") as DisplayObject));
			
			btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
			var img_char_background:DisplayObject = Client.getUIElement("img_friend_chat_bg") as DisplayObject
			setBackgroundDecorator(new AssetBackground(img_char_background));
			setSize(new IntDimension(img_char_background.width, img_char_background.height));
			setPreferredSize(new IntDimension(img_char_background.width, img_char_background.height));
			pack();
			setLocationXY((Engine.W - width)/2,(Engine.H - height)/2);
			//txtSelf.addActionListener(onBtnSend);
			txtSelf.setWordWrap(true);
			txtFriend.setWordWrap(true);
			txtFriend.setEditable(false);
			btn_friend_chat_send.addActionListener(onBtnSend);
			btn_friend_chat_close.addActionListener(onBtnClose);
			btn_friend_chat_send.setText("");
			btn_friend_chat_close.setText("");
			btn_friend_chat_motion.setText("");
			
			//btnMotion.wrapSimpleButton(Client.getUIElement("btn_motion") as SimpleButton);
			btn_friend_chat_send.wrapSimpleButton(Client.getUIElement("btn_friend_chat_send") as SimpleButton);
			btn_friend_chat_close.wrapSimpleButton(Client.getUIElement("btn_friend_chat_close") as SimpleButton);
			btn_friend_chat_motion.wrapSimpleButton(Client.getUIElement("btn_friend_chat_motion") as SimpleButton);
			
			btn_friend_chat_motion.setEnabled(false);
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 13, false, false, false, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			txtSelf.setFont(font);
			txtSelf.filters = new Array(filter);
			txtFriend.setFont(font);
			txtFriend.filters = new Array(filter);
			txtSelf.setForeground(ASColor.WHITE);
			txtFriend.setForeground(ASColor.WHITE);
			txtSelf.setBackgroundDecorator(null);
			txtFriend.setBackgroundDecorator(null);
			labelMessage.setFont(new ASFont("宋体", 13, true, false, false, false));
			labelMessage.setForeground(ASColor.WHITE);
			labelMessage.setTextFilters(new Array(filter));
			
			font = new ASFont("宋体", 12, false, false, false, false);
			labelFriendName.setFont(new ASFont("宋体", 12, false, false, true, false));
			labelFriendName.setForeground(ASColor.WHITE);
			labelFriendName.setTextFilters(new Array(filter));
			
			labelFriendRace.setFont(font);
			labelFriendRace.setForeground(ASColor.WHITE);
			labelFriendRace.setTextFilters(new Array(filter));
			
			labelFriendGender.setFont(font);
			labelFriendGender.setForeground(ASColor.WHITE);
			labelFriendGender.setTextFilters(new Array(filter));
			
			labelPrompt.setFont(font);
			labelPrompt.setForeground(ASColor.YELLOW);
			labelPrompt.setTextFilters(new Array(filter));
			//this.getTitleBar().getLabel().setForeground(ASColor.WHITE);
			//this.getTitleBar().getLabel().setBackground(ASColor.WHITE);
			//this.getTitleBar().getLabel().setMideground(ASColor.WHITE);
			//getTitleBar().getLabel().setFont(font);
			txtSelf.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown,false,0,true);
			txtSelf.addEventListener(KeyboardEvent.KEY_UP,onKeyUp,false,0,true);
			if(CharInfo.m_nGender == 0)
				txtSelf.setForeground(new ASColor(Engine.colorBoy));
			if(CharInfo.m_nGender == 1)
				txtSelf.setForeground(new ASColor(Engine.colorGirl));
			
			labelFriendName.addActionListener(onBtnFriendName);
		}
		private function onBtnFriendName(e:Event):void
		{
			if(_item == null)
			{
				return;
			}
			var obj:Object = new Object;
			obj.m_nCharId = int(_item.m_nCharId);
			obj.m_szCharName = _item.m_szCharName;
			obj.m_nLevel = int(_item.m_nLevel);
			obj.m_nRace = int(_item.m_nRace);
			obj.m_nGender = int(_item.m_nGender);
			
			var point:IntPoint = AsWingUtils.getStageMousePosition();
			FrameClickChar._objDest = obj;
			FrameClickChar._destCharId = int(obj.m_nCharId);
			FrameClickChar.show(_frame,point.x-_frame.x,point.y-_frame.y);
		}
		private var _pressedCtrl:Boolean = false;
		protected function onKeyDown(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case 17:
					_pressedCtrl = true;
					break;
				case 13:
					if(_pressedCtrl)
						onBtnSend(null);
					break;
				//trace("onKeyDown:"+e.keyCode.toString());
			}
		}
		
		protected function onKeyUp(e:KeyboardEvent):void
		{
			_pressedCtrl = false;
		}

		public static function show(item:Object):void
		{
			if(_frame == null)
			{
				_frame = new FrameFriendChat(item);
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMainHelp.getInstance().bringToTop(_frame);
			if(_messageArray[item.m_nCharId] == null)
				_messageArray[item.m_nCharId] = new String;
			_frame._item = item;
			
			_frame.update();
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
		public static var messages:Array = new Array;
		public static function phFriendChat_603(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phFriendChat_603", msg);
			var nIndex:uint = 0;
			var m_nCharId:uint = msg[nIndex++];//说话者
			var m_nLevel:uint = msg[nIndex++];//说话者
			var m_nRace:uint = msg[nIndex++];//说话者
			var m_nGender:uint = msg[nIndex++];//说话者
			var other_char_id:uint = msg[nIndex++];//说话者另一边
			var other_level:uint = msg[nIndex++];//说话者另一边
			var other_race:uint = msg[nIndex++];//说话者另一边
			var other_gender:uint = msg[nIndex++];//说话者另一边
			var name:String = msg[nIndex++];
			var time:String = msg[nIndex++];
			var message:String = msg[nIndex++];
			
			var dest_char_id:uint = m_nCharId;
			var dest_level:uint = m_nLevel;
			var dest_race:uint = m_nRace;
			var dest_gender:uint = m_nGender;
			if(m_nCharId == CharInfo.charId)
			{
				dest_char_id = other_char_id;
				dest_level = other_level;
				dest_race = other_race;
				dest_gender = other_gender;
				
				var array:Array = CharInfo.m_arFriends;
				for(var i:int = 0; i < array.length; i++)
				{
					var obj:Object = array[i];
					if(obj.m_nCharId == dest_char_id)
					{
						obj.love += 1;
					}
				}
			}
			
			if(_frame != null&& _frame.isShowing())
			{
				if(m_nCharId == _frame._item.m_nCharId || m_nCharId == CharInfo.charId)
				{
					if(_messageArray[dest_char_id] == null)
						_messageArray[dest_char_id] = new String;
					_messageArray[dest_char_id] += ('<font color="#999999">'+name + "  " + time +'</font><br>');
					_messageArray[dest_char_id] += ('<font color="#'+(m_nGender==0?Engine.colorBoyStr:Engine.colorGirlStr)+'">' + message+"</font><br>");
					_frame.txtFriend.setHtmlText(_messageArray[dest_char_id]);
					_frame.txtFriend.scrollToBottomLeft();
					return;
				}
			}

			var existed:Boolean = false;
			for each(var item:Object in messages)
			{
				if(item.m_nCharId == dest_char_id)
					existed = true;
			}
			if(!existed)
				messages.push({m_nCharId:dest_char_id,m_szCharName:name,m_nLevel:dest_level,m_nRace:dest_race,m_nGender:dest_gender,time:time,message:message});
			if(_messageArray[dest_char_id] == null)
				_messageArray[dest_char_id] = new String;
			_messageArray[dest_char_id] += ('<font color="#999999">'+name + "  " + time +"</font><br>");
			_messageArray[dest_char_id] += ('<font color="#'+(m_nGender==0?Engine.colorBoyStr:Engine.colorGirlStr)+'">' +message+"</font><br>");
			PanelMainHelp.getInstance().flashFriendButton();
		}
		protected function onBtnSend(e:Event):void
		{
			
			if(txtSelf.getText().length == 0)
				return;
			Client.getInstance().sendPacketLogic(603,[_item.m_nCharId,txtSelf.getText()]);

			txtSelf.setText("");
		}
		protected function onBtnClose(e:Event):void
		{
			closeReleased();
		}
		
		 
	}
}